package array;

public class ValueEqualToIndex {

	// Problem 1.4
	// given array has sorted distinct values
	// it finds first value which is equal to its index
	public static int findIndex(int[] arr){
		
		int begin = 0;
		int end = arr.length -1;
		
		while(begin <= end){
			
			int mid = begin + (end - begin)/2;
			
			if(arr[mid] == mid){
				return mid;
			}else if(arr[mid] > mid){
				end = mid -1;
			}else{
				begin = mid + 1;
			}
			
		}
		
		return Integer.MIN_VALUE;
	}
}
